package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.common.utils.DateUtils;
import com.itheima.common.utils.POIUtils;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Autowired
    OrderSettingService orderSettingService;
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 模板下载
     */
    @GetMapping("/download")
//    public void write(String fileName) throws Exception{
    public void write(@RequestParam String filename) throws Exception {
        // 创建 Excel 工作簿
        XSSFWorkbook excel = new XSSFWorkbook();
        XSSFSheet sheet = excel.createSheet("预约设置");
        XSSFRow row1 = sheet.createRow(0);
        //创建单元格并在单元格中设置值，单元格编号也是从0开始，1表示第2个单元格
        row1.createCell(1).setCellValue("日期");
        row1.createCell(2).setCellValue("人数");
//        sheet.createRow(0).createCell(2).setCellValue("日期");
//        sheet.createRow(0).createCell(3).setCellValue("人数");
        String path = "D:/" + filename;
        FileOutputStream out = new FileOutputStream(new File(path));
        excel.write(out);
        out.flush();
        out.close();
        excel.close();
        System.out.println("ok");
    }

    /**
     * 根据日期修改可预约人数
     */
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByOrderDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据年月查询对应的预约设置信息
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){

        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(month);
            //返回结果
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,list);
        } catch (Error e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_LIST_FAIL);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件上传
     * @param excelFile 页面传过来的对象
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){

        //1.调用工具类的上传方法
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            //2.做非空判断
            if (list != null && list.size() > 0 ){
                //3.将list集合的泛型改为ordersetting类型
                ArrayList<OrderSetting> orderSettings = new ArrayList<>();
                //4.遍历list集合
                for (String[] strings : list) {
                    //5.创建一个ordersetting对象,将arraylist中的数据转入orderseting中
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]),Integer.parseInt(strings[1]));
                    //6.将其添加到array list集合中
                    orderSettings.add(orderSetting);
                }
                //7.调用service的方法
                orderSettingService.add(orderSettings);
                return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return null ;
    }

    }
